﻿(async () => {
    await window.ris.initialize();
    $(() => {
        const $eventEmitter = $('body > .container');
        let reloadGrid = () => {
            dataGrid.refresh();
        };
        const advancedSearch = $('.advanced-search').advancedSearchWithCollapse(null, (e) => {
            reloadGrid = () => {
                dataGrid.refresh({ userData: e });
            };
            reloadGrid();
        });
        advancedSearch.expand();
        const dataGrid = $('.exam-grid').dataGrid({
            onLoad(loadOptions) {
                return new Promise((resolve, reject) => {
                    $.post({
                        dataType: 'json',
                        contentType: 'application/json;charset=UTF-8',
                        url: `/api/match/get-studies`,
                        data: JSON.stringify(loadOptions),
                        success: result => {
                            if (result.success) {
                                result.data.columns = [{
                                    dataField: "oid",
                                    visible: false
                                }, {
                                    dataField: "studyKey",
                                    visible: false
                                }, {
                                    dataField: "accessionNumber",
                                    caption: "检查号",
                                    width: 150
                                }, {
                                    dataField: "patientName",
                                    caption: "姓名",
                                    width: 150
                                }, {
                                    dataField: "patientSex",
                                    caption: "性别",
                                    width: 80
                                }, {
                                    dataField: "patientAge",
                                    caption: "年龄",
                                    width: 80
                                }, {
                                    dataField: "patientId",
                                    caption: "患者Id",
                                    width: 150
                                }, {
                                    dataField: "studyId",
                                    caption: "检查Id",
                                    width: 150
                                }, {
                                    dataField: "deviceName",
                                    caption: "设备",
                                    width: 80
                                }, {
                                    dataField: "studyDateTime",
                                    caption: "检查日期",
                                    dataType: "datetime",
                                    width: 150
                                }, {
                                    dataField: "manufacturer",
                                    caption: "制造商"
                                }, {
                                    dataField: "manufacturerModelName",
                                    caption: "制造商型号"
                                }];
                                resolve(result)
                            } else {
                                reject(result.error);
                            }
                        }
                    });
                })
            }
        });

        const tools = $.getTechnicianTools($eventEmitter, null, advancedSearch, dataGrid, {
            reloadGrid: () => {
                if (reloadGrid instanceof Function) {
                    reloadGrid();
                }
            }
        });

        const toolbar = $('.toolbar').dxToolbar({}).dxToolbar('instance');
        //判断是否已经启用了自动刷新
        tools.autoRefresh.options.text = autoRefresh('match_refreshInterval', 'match_interval', () => {
            if (reloadGrid instanceof Function) {
                reloadGrid();
            }
        }, true);
        tools.refreshSetting.options.items = [tools.match_autoRefresh.options, tools.match_refreshInterval.options]
        toolbar.option('items', [{
            ...tools.match_select,
            options: {
                ...tools.match_select.options,
                type: 'default',
                stylingMode: 'contained',
            }
        }, {
            ...tools.match_select_report,
                options: {
                    ...tools.match_select_report.options,
                    type: 'default',
                    stylingMode: 'contained',
                }
            }, tools.refresh, tools.refreshSetting]);

        $.get('/api/match/get-study-advanced-search-view').then(result => {
            if (result.success) {
                if (result.view) {
                    advancedSearch.updateView(result.view, result.data);
                }
            }
        });
    });
})();